﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CKEditor.NET;
using MedicWeb.MedicService;

namespace MedicWeb.forms.Print.Edit {
    public partial class TemplateEdit : SessionPage {

        private PrintTemplate template;

        protected void Page_Load(object sender, EventArgs e) {
            string type = Request.QueryString["type"];
            //Form.Action = ResolveUrl("~/forms/Editors/Directory/JobEdit.aspx?type="+type);
            string id = Request.QueryString["id"];
            var ms = new MainService();
            if (id != null) {
                //Form.Action += "&id=" + id;
                template = ms.LoadPrintTemplate(GetSession(), type, Convert.ToInt32(id));
            } else {
                template = new PrintTemplate();
                template.Type = type;
            }
            if ("MedicService.Domain.Print.PrintExtract".Equals(type)) {
                hfTypes.Value = "MedicService.Domain.Doctor";
            } else if ("MedicService.Domain.Print.PrintOrder".Equals(type)) {
                hfTypes.Value = "MedicService.Domain.Order,MedicService.Domain.Doctor";
            } else if ("MedicService.Domain.Print.PrintProfileProtocol".Equals(type)) {
                hfTypes.Value = "MedicService.Domain.Doctor";
            } else if ("MedicService.Domain.Print.PrintAttestatProtocol".Equals(type)) {
                hfTypes.Value = "MedicService.Domain.GroupExpMember,MedicService.Domain.Doctor";
            } else if ("MedicService.Domain.Print.PrintSertificate".Equals(type)) {
                hfTypes.Value = "MedicService.Domain.Doctor";
            }
            if (!IsPostBack) {
                Editor.Text = template.Template;
                Editor.Controls.Add(new DropDownList());
                tfTitle.Text = template.Title;
                tfMarginDown.Text = "" + template.MarginBottom;
                tfMarginLeft.Text = "" + template.MarginLeft;
                tfMarginRight.Text = "" + template.MarginRight;
                tfMarginTop.Text = "" + template.MarginTop;
                comboDoubleSided.SelectedValue = "" + template.DoubleSided;
                comboFormat.SelectedValue = template.Format;
                comboMode.SelectedValue = template.Mode;
                comboOrientation.SelectedValue = template.Orientation;
                comboDefault.SelectedValue = template.Active ? "1" : "0";
            }
        }

        protected void btnSave_Click(object sender, EventArgs e) {
            template.Template = Editor.Text;
            template.Title = tfTitle.Text;
            template.DoubleSided = Convert.ToInt32(comboDoubleSided.SelectedValue);
            template.Format = comboFormat.SelectedValue;
            template.MarginBottom = Convert.ToSingle(tfMarginDown.Text.Replace(".", ","));
            template.MarginFooter = 0;
            template.MarginHeader = 0;
            template.MarginLeft = Convert.ToSingle(tfMarginLeft.Text.Replace(".", ","));
            template.MarginRight = Convert.ToSingle(tfMarginRight.Text.Replace(".", ","));
            template.MarginTop = Convert.ToSingle(tfMarginTop.Text.Replace(".", ","));
            template.Mode = comboMode.SelectedValue;
            template.Orientation = comboOrientation.SelectedValue;
            template.Active = comboDefault.SelectedValue == "1";
            var ms = new MainService();
            bool redirect = template.Id == 0;
            var printTemplate = ms.SavePrintTemplate(GetSession(), template);
            if (comboDefault.SelectedValue == "1") {
                ms.SetTemplateActive(GetSession(), printTemplate);
            }
            Response.Redirect(ResolveUrl("~/forms/Print/Edit/TemplateEdit.aspx?type=" + printTemplate.Type + "&id=" + printTemplate.Id));
        }
    }
}